package net.lainiao.morethread.Chapter1.demo4;

import java.util.Date;
import java.util.Deque;

/**
 * Created by Administrator on 2017/2/27.
 */
public class CleanerTask extends Thread {
    private Deque<Event> deque;
    public CleanerTask(Deque<Event> deque){
        this.deque=deque;
        setDaemon(true);
    }

    @Override
    public void run() {
        while (true){
            Date date=new Date();
            clean(date);
        }
    }

    private void clean(Date date) {
        long difference;
        boolean delete;
        if(deque.size()==0){
            return;
        }
        delete=false;
        do{
            Event e=deque.getLast();
            difference=date.getTime()-e.getActionDate().getTime();
            if(difference>10000){
                System.out.println("clear :"+e.getEventName());
                deque.removeLast();
                delete=true;
            }
        }while (difference>10000);
        if(delete){
            System.out.println("Clear:Size of the queue:"+deque.size());
        }
    }
}
